program testmulttime
use mkl95_blas
use mkl95_precision
implicit none
integer:: nD = 250000
real(8), allocatable, dimension(:) :: a, b
complex(16), allocatable, dimension(:) :: c, d, e
double precision, dimension(:), allocatable::f
real(8):: g
integer:: i, j
double precision, external:: ddot
allocate( a(nD) )
allocate( b(nD) )
allocate( c(nD) )
allocate( d(nD) )
call init_random_seed()
call random_number(a)
call random_number(b)
c = cmplx(a,b);
d = cmplx(b,-a);
allocate( e(nD) )
allocate( f(nD) )
print *, "OK"
call GetTime
do j = 1, 100
  ! e = c*d
  ! g = dot_product(b,a)
  ! g = dot_product(c,d)
  g = ddot(nD, 1, b, 1, a)
  ! do i = 1, nD
    ! ! if( mod(i,100000) == 0 ) then
    ! ! print *, i
    ! ! end if
    ! ! f(i) = real(c(i))*real(d(i)) - aimag(c(i))*aimag(d(i))
    ! ! f(i) = real(c(i))*aimag(d(i)) + aimag(c(i))*real(d(i))
    ! f(i) = a(i)*b(i) + b(i)*b(i)
    ! f(i) = a(i)*a(i) - b(i)*a(i)
  ! end do
end do
call GetTime
deallocate( a, b )
deallocate( c, d, e, f )
end program

SUBROUTINE init_random_seed()
  INTEGER :: i, n, clock
  INTEGER, DIMENSION(:), ALLOCATABLE :: seed

  CALL RANDOM_SEED(size = n)
  ALLOCATE(seed(n))

  CALL SYSTEM_CLOCK(COUNT=clock)

  seed = clock + 37 * (/ (i - 1, i = 1, n) /)
  CALL RANDOM_SEED(PUT = seed)

  DEALLOCATE(seed)
END SUBROUTINE



SUBROUTINE GetTime
  USE IFPORT, only: ETIME
  
  real(kind=8) :: utime_sec,stime_sec ! time in seconds

  ! variables for intel keeping track of time
  real(4) :: elapsed_time
  real(4),dimension(2) :: time_array
  
  ! calculate elapsed time
  ! elapsed_time = time_array(1) + time_array(2)
  elapsed_time = ETIME(time_array)

  utime_sec = real(time_array(1),8)
  stime_sec = real(time_array(2),8)


  ! write it out
  write(unit=*,fmt='("******************** Time Statistics ********************")')
  write(unit=*,fmt='("User Time:",F20.2," sec",/,"Sys Time:",F20.2," sec")') utime_sec,stime_sec
  write(unit=*,fmt='("*********************************************************")')


END SUBROUTINE GetTime
